package com.qcby.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qcby.admin.form.TagAddForm;
import com.qcby.admin.form.TagEditForm;
import com.qcby.admin.form.TagForm;
import com.qcby.admin.model.SgTag;
import com.qcby.admin.service.SgTagService;
import com.qcby.admin.vo.PageVo;
import com.qcby.admin.vo.TagArticleVO;
import com.qcby.framwork.utils.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 标签 前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-09-07
 */
@RestController
@RequestMapping("/content/tag")
public class SgTagController {
    @Autowired
    private SgTagService sgTagService;

    /**
     * 查询标签列表
     * @param tagForm
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<PageVo> list(TagForm tagForm){
        return sgTagService.pageTagList(tagForm);
    }

    /**
     * 添加标签
     * 注意创建时间、修改时间、创建人、修改人的自动递增（需要登录后获取到session）
     * @param tagAddForm
     * @return
     */
    @PostMapping
    public ResponseResult add(@RequestBody TagAddForm tagAddForm){
        SgTag sgTag=new SgTag();
        BeanUtils.copyProperties(tagAddForm,sgTag);
        Boolean res=sgTagService.save(sgTag);
        if(!res)
            return ResponseResult.errorResult(0,"添加失败");
        return ResponseResult.okResult();
    }

    /**
     * 删除标签
     * 并不是真正的删除，数据库中还是有这条数据，只是修改了逻辑删除字段的值改为1
     * 进行批量删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public ResponseResult remove(@PathVariable(value = "id")Long[] id) {
        QueryWrapper<SgTag> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("id",id);
        sgTagService.remove(queryWrapper);
        return ResponseResult.okResult();
    }

    /**
     *进行回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult getInfo(@PathVariable ("id") Long id){
        SgTag sgTag=sgTagService.getById(id);
        return ResponseResult.okResult(sgTag);
    }

    /**
     *标签修改
     * @param tagEditForm
     * @return
     */
    @PutMapping
    public ResponseResult edit(@RequestBody TagEditForm tagEditForm){
        SgTag sgTag=new SgTag();
        BeanUtils.copyProperties(tagEditForm,sgTag);
        Boolean res=sgTagService.updateById(sgTag);
        if(!res)
            return ResponseResult.errorResult(111,"修改失败");
        return ResponseResult.okResult();
    }

    /**
     * 文章模块的标签查询
     * @return
     */
    @GetMapping("/listAllTag")
    public ResponseResult getListAllTag(){
        //查找没有被删除的标签
        QueryWrapper<SgTag> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("del_flag","0");
        //转换成list集合
        List<SgTag> sgTagList=sgTagService.list(queryWrapper);
        //返回页面的数据
        List<TagArticleVO> tagArticleVOS=new ArrayList<>();
        for(SgTag sgTag:sgTagList){
            TagArticleVO tagArticleVO=new TagArticleVO();
            BeanUtils.copyProperties(sgTag,tagArticleVO);
            tagArticleVOS.add(tagArticleVO);
        }
        return ResponseResult.okResult(tagArticleVOS);
    }
}

